home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Headers / foundation / NSNotification.h < prev    next >
Text File  |  1994-05-02  |  3KB  |  81 lines

  1. /*    NSNotification.h
  2.       Copyright 1993, 1994, NeXT, Inc.
  3.     Posting and observing notifications;
  4.     NeXT, June 93
  5. */
  6.  
  7. #import <foundation/NSDictionary.h>
  8.  
  9. /* Example of use:
  10.     Say you have a port object that is dying and an object (observer) that needs to know about the death of that port.
  11.     The observer would initially register as:
  12.     
  13.     [[NSNotificationCenter defaultCenter] addObserver:observer selector:@selector(handlePortDeath:) forNotificationName:@"NSPortInvalid" fromObject:port];
  14.     
  15.     When a port dies, the following code gets executed:
  16.     
  17.     [[NSNotificationCenter defaultCenter] postNotificationName:@"NSPortInvalid" object:port];
  18.     
  19.     The notification center will then perform:
  20.     
  21.     - (void)handlePortDeath:(NSNotification *)notification {
  22.     // we know that [port isEqual:[notification object]]
  23.     ...
  24.     }
  25.  
  26. */
  27.     
  28. /***************    Notification    ***************/
  29.  
  30. @interface NSNotification:NSObject <NSCopying>
  31.     /* Note that notifications may contain extra information, but then that extra data must be agreed upon between notifiers and observers */
  32.  
  33. - (NSString *)notificationName;
  34.     /* A string denoting the notification such as "NSPPLChanged" or "NSPortInvalid" */
  35.     
  36. - notificationObject;
  37.     /* The object of the notification; often is the object that posted a notification about itself;
  38.     may be nil */
  39.  
  40. + allocWithZone:(NSZone *)zone;
  41.     /* Creates an instance of a concrete class (substitutes a concrete class if called with NSNotification) */
  42.  
  43. + (NSNotification *)notificationWithName:(NSString *)name object:object;
  44.     /* copies name; retains object */
  45.  
  46. @end
  47.  
  48. /***************    Notification Center    ***************/
  49.  
  50. @interface NSNotificationCenter:NSObject {
  51.     id            _lock;
  52.     NSMutableDictionary    *_registry;
  53.     id            _noNotificationNameRegistry;
  54. }
  55.  
  56. + (NSNotificationCenter *)defaultCenter;
  57.     /* a per-task notification center used for generic notifications */
  58.     
  59. - (void)postNotification:(NSNotification *)notification;
  60.  
  61. - (void)addObserver:observer selector:(SEL)selector notificationName:(NSString *)notificationName object:object;
  62.     /* observer will perform selector with the notification as argument when notification with given name from given object is posted;
  63.     observer is not retained which implies that removeObserver: must be called prior to invalidating the observer;
  64.     If object is nil, observer will get posted whatever the object was;
  65.     object (when non-nil) is not retained by the notification center which implies that removeObserver: must be called prior to invalidating the object;
  66.     object identity is pointer equality;
  67.     If notificationName is nil, observer will get posted whatever for all notifications that match object */
  68.  
  69. - (void)removeObserver:observer notificationName:(NSString *)notificationName object:object;
  70.     /* This method will remove all observers with same notificationName and same object (even when object is nil) */ 
  71.  
  72. - (void)removeObserver:observer;
  73.     /* Removes all the observations of observer;
  74.     Relativly slow (goes over all tables) */
  75.  
  76. - (void)postNotificationName:(NSString *)notificationName object:object;
  77.     /* Short cut for posting an notification */
  78.  
  79. @end
  80.  
  81.